package com.lxs.DayDayUp;

import com.lxs.DayDayUp_Mid.R;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class Main extends Activity {
	/** Called when the activity is first created. */
	Button studynButton, studyrButton, moresoftButton,musicbButton;
	public MediaPlayer mediaPlayer;
	boolean flag = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlay);
		allfindViewById();
		allclick();
		mediaPlayer = MediaPlayer.create(this, R.raw.mu);

		mediaPlayer.setLooping(true);

		mediaPlayer.start();
		
	}

	@Override
	public void onDestroy(){  
        super.onDestroy();  
	       if (mediaPlayer != null) {  
	    	   mediaPlayer.stop();  
	    	   mediaPlayer.release();  
	    	   mediaPlayer = null;  
	        }    
 
    }  

	private void allclick() {
		// TODO Auto-generated method stub
		studynButton.setOnClickListener(studynButton_clk);
		studyrButton.setOnClickListener(studyrButton_clk);
		moresoftButton.setOnClickListener(moresoftButton_clk);
		musicbButton.setOnClickListener(musicbButton_clk);
	}

	private void allfindViewById() {
		// TODO Auto-generated method stub
		studynButton = (Button) findViewById(R.id.studyn);
		studyrButton = (Button) findViewById(R.id.studyr);
		moresoftButton = (Button) findViewById(R.id.moresoft);
		musicbButton=(Button)findViewById(R.id.bmusic);

		
	}

	private Button.OnClickListener studynButton_clk = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent in = new Intent(Main.this, DayDayUpActivity.class);
			startActivity(in);
		}

	};
	private Button.OnClickListener musicbButton_clk = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

	        if (flag) {
				musicbButton.setBackgroundResource(R.drawable.nomusic);
		        if(mediaPlayer.isPlaying()){  
		        	mediaPlayer.pause();  
		        }
				flag=false;
			} else {
				musicbButton.setBackgroundResource(R.drawable.muisc1);
//		        if(!mediaPlayer.isPlaying()){  

		        	mediaPlayer.start();
//		        }
				flag=true;
			}
		}

	};
	private Button.OnClickListener studyrButton_clk = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent in = new Intent(Main.this, studyradom.class);
			startActivity(in);
		}

	};
	private Button.OnClickListener moresoftButton_clk = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			initPopWindow();
		}

	};
	private void initPopWindow() {
		View contentView = LayoutInflater.from(getApplicationContext())  
        .inflate(R.layout.about, null); 
		contentView.setBackgroundColor(R.string.colora);
//		contentView.setBackgroundResource(R.drawable.whitebg);
		final PopupWindow popupWindow = new PopupWindow(  
                findViewById(R.id.mainlout), 480, 260);
		popupWindow.setContentView(contentView);  
		 popupWindow.setFocusable(true);
		 popupWindow.showAsDropDown(moresoftButton);
		 
		 Button bab1 = (Button) contentView.findViewById(R.id.bab1);
		 bab1.setOnClickListener(new OnClickListener() {  
	            @Override  
	            public void onClick(View v) {  
	                popupWindow.dismiss();  
	            }  
	        });  
	}
}